package com.controller;

import com.bean.dangBean.Balance;
import com.service.BalanceService;
import com.util.PageAssistant;
import com.vo.BalanceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("bal")
public class BalanceController {
    public  static Double balance1 =0.0;//存取结果集的当前余额

    @Autowired
    private BalanceService balanceService;

    @RequestMapping("query")
    public String query(Model model, HttpSession session, BalanceVo balQueryInfo,
                        PageAssistant<Balance> paBalance){
        System.out.println(">>> balance query");
        System.out.println(balQueryInfo);
        System.out.println(paBalance);
        System.out.println("------------------------------------------------");

        //创建参数集合
        Map<String, Object> map = new HashMap<>();
        //添加查询参数


        //查询总记录数
        paBalance.setCount(balanceService.count(map));

        //设置分页参数
        map.put("start", paBalance.getStart());
        map.put("size", paBalance.getSize());


        //查询当前页内容
        paBalance.setItems(balanceService.query(map));
        //保存
        model.addAttribute("paBalance", paBalance);

        if (paBalance.getCurr()==1 && paBalance.getItems().get(0).getBalance()!= null ){
            balance1=paBalance.getItems().get(0).getBalance();
        }

        model.addAttribute("balance", balance1);

        System.out.println("balance1为:"+balance1);
        return "page/bal/balanceList";
    }

   /* @RequestMapping("unique")
    public String unqiue(Model model,HttpSession session, BalanceVo bal, String page){
        System.out.println(">>> con unique");
        System.out.println(bal);
        System.out.println(page);
        System.out.println("------------------------------------------------");

        //查询
        List<Balance> balList = balanceService.find(bal.parse());

        //保存
        session.setAttribute("balInfo", balList.get(0));
        model.addAttribute("balInfo", balList.get(0));
        System.out.println("balList为"+balList.get(0));
        //判断, page是否为null
//        page = page == null ? "detail.jsp" : page;
        if (page == null){
            page = "balDetail.html";
        }

        return "/page/bal/" + page;
    }*/

    @RequestMapping("remove")
    public String remove(HttpSession session, Balance balance){
        System.out.println(">>> bal remove");
        System.out.println(balance);
        System.out.println("------------------------------------------------");

        //删除
        int count = balanceService.remove(balance);

        //保存
        String updateMsg = "删除成功!";
        String updateClass = "update-suc";
        if(count != 1){
            updateMsg = "删除失败!";
            updateClass = "update-err";
        }
        session.setAttribute("updateMsg", updateMsg);
        session.setAttribute("updateClass", updateClass);

        return "redirect:/bal/query";

    }

    @RequestMapping("modify")
    public String modify(HttpSession session, BalanceVo balanceVo){
        System.out.println(">>> bal modify");
        System.out.println(balanceVo);
        System.out.println("------------------------------------------------");

        //修改
        int count = balanceService.modify(balanceVo.parse());


        return "redirect:/bal/unique?id="+balanceVo.getId(); }

    @RequestMapping("save")
    public String save(HttpSession session, Balance bal){
        System.out.println(">>> bal add");
        System.out.println("bal为"+bal.getBalance());
        System.out.println("------------------------------------------------");
        System.out.println("balance1为"+balance1);
        bal.setTime(new Date());
        Double total = bal.getBalance() + balance1;
        System.out.println(total);

        bal.setBalance(total);

        System.out.println(bal+"5吞吞吐吐");
        //添加
        int count = balanceService.save(bal);
        return "redirect:/bal/query";
    }

}
